package sandbox.vaadin.skiller;

import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import sandbox.vaadin.jpa.MutableLocalEntityProviderAbstract;
import sandbox.vaadin.skiller.entity.Skillgroup;

@Stateless
@TransactionManagement
public class MutableLocalEntityProviderSkillgroupSSB
		extends MutableLocalEntityProviderAbstract<Skillgroup>
{
	@PersistenceContext
	private EntityManager entityManager;

	public MutableLocalEntityProviderSkillgroupSSB()
	{
		super(Skillgroup.class);
//		// this entity provider will not handle transactions, transactions are
//		// handled by the ejb container
//		setTransactionsHandledByProvider(false);
	}

	@Override
  public EntityManager getEntityManager()
  {
	  return entityManager;
  }

	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
  protected void runInTransaction(Runnable operation)
  {
    super.runInTransaction(operation);
  }
	
	@PostConstruct
	public void init()
	{
		setEntityManager(getEntityManager());

// TODO move to constructor?
// TODO move to subclass?
//		// this entity provider will not handle transactions, transactions are
//		// handled by the ejb container
		setTransactionsHandledByProvider(false);

		// entityManager is transaction-scoped, which means that the entities will
		// be automatically detached when the transaction is closed. Therefore we
		// do not need to explicitly detach them.
		setEntitiesDetached(false);
	}
}